package otherSAMS.ui;
import otherSAMS.dao.UserDao;
import otherSAMS.model.User;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*
 * Created by JFormDesigner on Thu Dec 29 10:51:21 CST 2022
 */



/**
 * @author txx
 */
public class UserLogin extends JFrame{
        public static void main (String[] args){
            UserLogin  login  = new UserLogin();
            login.initComponents();
            login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            login.setVisible(true);
        }

       /* public UserLogin() {
            initComponents();
        }*/

        private void okButtonMouseClicked(MouseEvent e) {
            String username = usernameField.getText();//用户名
            String password = new String(passwordField.getPassword());//密码
            UserDao userDao = new UserDao();
            User user = userDao.selectByUsername(username);
            if(user != null && user.getPassword().equals(password)){
                JOptionPane.showMessageDialog(this.getContentPane(),"登录成功,学生成绩管理系统欢迎您！");
                //隐藏登录界面
                this.dispose();
                if(2==user.getType()) {
                    TeacherScore teacherManage = new TeacherScore();
                    teacherManage.setVisible(true);
                }else if(1==user.getType()){
                    StudentScore studentScore = new StudentScore();
                    studentScore.setVisible(true);
                }else{
                    UserManage userManage  = new UserManage();
                    userManage.setVisible(true);
                }
            }else{
                JOptionPane.showMessageDialog(this.getContentPane(),"用户名或密码错误");
                usernameField.setText(" ");
                passwordField.setText(" ");

            }

            // TODO add your code here
        }

        private void cancelButtonMouseClicked(MouseEvent e) {
            // TODO add your code here
            this.dispose();
        }

        void initComponents() {
            // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
            contentPanel = new JPanel();
            usernameField = new JTextField();
            label1 = new JLabel();
            label2 = new JLabel();
            label3 = new JLabel();
            label4 = new JLabel();
            passwordField = new JPasswordField();
            buttonBar = new JPanel();
            okButton = new JButton();
            cancelButton = new JButton();

            //======== this ========
            setTitle("\u5b66\u751f\u6210\u7ee9\u7ba1\u7406");
            setForeground(SystemColor.window);
            setBackground(new Color(0x99ccff));
            Container contentPane = getContentPane();

            //======== contentPanel ========
            {
                contentPanel.setBackground(new Color(0xccffff));

                //---- label1 ----
                label1.setText("\u7528\u6237\u540d");
                label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));

                //---- label2 ----
                label2.setText("\u5bc6\u7801");
                label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));

                //---- label4 ----
                label4.setText("\u7cfb\u7edf\u767b\u5f55");
                label4.setForeground(Color.blue);
                label4.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 22));

                GroupLayout contentPanelLayout = new GroupLayout(contentPanel);
                contentPanel.setLayout(contentPanelLayout);
                contentPanelLayout.setHorizontalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPanelLayout.createSequentialGroup()
                            .addContainerGap(98, Short.MAX_VALUE)
                            .addGroup(contentPanelLayout.createParallelGroup()
                                .addComponent(label1)
                                .addComponent(label2))
                            .addGap(18, 18, 18)
                            .addGroup(contentPanelLayout.createParallelGroup()
                                .addComponent(usernameField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE)
                                .addGroup(contentPanelLayout.createSequentialGroup()
                                    .addGap(105, 105, 105)
                                    .addComponent(label3))
                                .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
                            .addGap(46, 46, 46))
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addGap(160, 160, 160)
                            .addComponent(label4)
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                );
                contentPanelLayout.setVerticalGroup(
                    contentPanelLayout.createParallelGroup()
                        .addGroup(contentPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(label3)
                            .addGap(36, 36, 36)
                            .addComponent(label4)
                            .addGap(18, 18, 18)
                            .addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(usernameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label1))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(contentPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label2)
                                .addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(60, Short.MAX_VALUE))
                );
            }

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setForeground(Color.lightGray);
                buttonBar.setBackground(Color.cyan);
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u786e\u8ba4\u767b\u5f55");
                okButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        okButtonMouseClicked(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u9000\u51fa");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        cancelButtonMouseClicked(e);
                    }
                });
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }

            GroupLayout contentPaneLayout = new GroupLayout(contentPane);
            contentPane.setLayout(contentPaneLayout);
            contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(contentPaneLayout.createParallelGroup()
                            .addComponent(contentPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(buttonBar, GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE))
                        .addContainerGap())
            );
            contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                    .addGroup(contentPaneLayout.createSequentialGroup()
                        .addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonBar, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
            );
            pack();
            setLocationRelativeTo(getOwner());
            // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
        }

        // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
        private JPanel contentPanel;
        private JTextField usernameField;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JLabel label4;
        private JPasswordField passwordField;
        private JPanel buttonBar;
        private JButton okButton;
        private JButton cancelButton;
        // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    }


